package com.dotto.atos.services.actions;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;

import com.dotto.atos.models.City;
import com.dotto.atos.models.Branch;
import com.dotto.atos.utils.LogUtil;

public class BranchService {

	public static final String TAG = "BranchService>";
	private static final String FILE_NAME = "il_ilce.json";
	private static HashMap<String, City> citylist;
	private static HashMap<String, Branch> branchList;

	public static void init(Activity activty) {
		if (activty == null)
			return;
		InputStream is = null;
		try {
			is = activty.getAssets().open(FILE_NAME);
			int size = is.available();
			byte[] buffer = new byte[size];
			is.read(buffer);
			String bufferString = new String(buffer);
			JSONArray jsonArray = new JSONArray(bufferString);
			if (jsonArray != null) {
				citylist = new HashMap<String, City>();
				branchList = new HashMap<String, Branch>();
				int length = jsonArray.length();
				JSONObject obj = null;
				Branch branch = null;
				City city = null;
				for (int i = 0; i < length; i++) {
					obj = null;
					branch = null;
					city = null;
					obj = jsonArray.getJSONObject(i);
					if (obj != null) {
						branch = new Branch(obj);
						if (branch != null && branch.getId() > 0) {
							city = null;
							if (citylist.containsKey(branch.getCityId() + "")) {
								city = citylist.get(branch.getCityId());
							}
							if (city == null)
								city = new City(obj);

							if (city != null && city.getId() > 0) {
								city.putProvince(branch);
								citylist.put(city.getId() + "", city);
							}
							branchList.put(branch.getId() + "", branch);
						}
					}
				}
				jsonArray = null;
				obj = null;
				branch = null;
				city = null;
			}
		} catch (Exception e) {
			LogUtil.logError(TAG + "init", "", e);
		} finally {
			try {
				if (is != null)
					is.close();
			} catch (Exception e2) {
				LogUtil.logError(TAG + "init", "is close", e2);
			}
		}
	}

	public static City getCity(int id) {
		String key = id + "";
		if (citylist != null && citylist.containsKey(key)) {
			return citylist.get(key);
		}
		return null;
	}

	public static Branch getBranch(int id) {
		String key = id + "";
		if (branchList != null && branchList.containsKey(key)) {
			return branchList.get(key);
		}
		return null;
	}

	public static List<Branch> getBranchList() {
		List<Branch> list = new ArrayList<Branch>();
		if (branchList != null && branchList.size() > 0) {
			Iterator<Entry<String, Branch>> ittCity = branchList.entrySet()
					.iterator();
			if (ittCity != null)
				while (ittCity.hasNext()) {
					Branch b = ittCity.next().getValue();
					list.add(b);
				}
		}
		return list;
	}

	public static List<City> getCityList() {
		List<City> list = new ArrayList<City>();
		if (citylist != null && citylist.size() > 0) {
			Iterator<Entry<String, City>> ittCity = citylist.entrySet()
					.iterator();
			if (ittCity != null)
				while (ittCity.hasNext()) {
					City c = ittCity.next().getValue();
					list.add(c);
				}
		}
		return list;
	}

}
